<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
				<view class="logo-text">
					<text>数字食安</text>
				</view>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="用户名/手机号码"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				bgColor="linear-gradient(to right, rgba(150,150,100,0.7), rgba(0,0,0,0.6))"
				:rotate="isRotate" 
				@click="startLogin"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {mapGetters, mapMutations, mapActions} from 'vuex'
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '../../static/img/other/appicon.png',
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
				isSilent: false
			};
		},
		components:{
			wInput,
			wButton,
		},
		computed: {
			...mapGetters(['hasLogin', 'sysId', 'orgId'])
		},
		onLoad() {
			this.isLogin();
		},
		methods: {
			...mapActions(['setUserInfo', 'setHasLogin', 'setSysId', 'setOrgId', ]),
			isLogin(){
				//判断缓存中是否登录过，直接登录
				try {
					const value = this.hasLogin || uni.getStorageSync('hasLogin');
					console.log('value', value)
					if (value) {
						//有登录信息
						console.log("已登录用户：",value);
						this.setHasLogin(true); //存入状态
						if (this.sysId && this.orgId) {
							uni.redirectTo({
								url: '../tabbar/work/work',
							})
						} else {
							uni.redirectTo({
								url: '../identityChoose/identityChoose',
							})
						}
					}
				} catch (e) {
					// error
				}
			},
		    startLogin(e){
                console.log(e)
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					this.showToast('用户名不能为空')
				    return;
				}
		        if (this.passData.length < 5) {
					this.showToast('密码不正确!')
		            return;
		        }				
				this.isRotate = true
				// 登录接口验证 1.验证用户名和密码是否合法
				this.$request.ehomeLogin({
					'loginName': this.phoneData,
					'pwd': this.passData
				}).then(res => {
					// 如果返回空的话，就是用户名，密码不正确
					if (res === "[object Object]") {
						this.showToast('用户名或者密码错误!')
						setTimeout(() => {
							this.isRotate = false
						}, 800)
					} else {
						// 存储用户信息
						if (typeof res !== 'string') {
							this.showToast('用户名或者密码错误!')
							setTimeout(() => {
								this.isRotate = false
							}, 800)
							return
						}
						setTimeout(() => {
							this.isRotate = false
						}, 800)
						// 设置用户信息
						this.setUserInfo(JSON.parse(res))
						// 体系和组织的选择
						// this.verifySystem()	
						this.getOrgAndSySList()			
					}
				}).catch(err => {
					console.log('err', err)
					this.isRotate = false
				})
		    },
			/**
			 * @description 判断体系和组织
			*/
			getOrgAndSySList() {
				this.$request.ygstConstraManageManagerLoginListGet().then(res => {
					const data = res
					if (data && data.length == 0) {
						this.showToast('您没有权限登录!')
					} else {
						/*
						 * 1.如果组织和体系只有一个，就默认选择登录
						 * 2.多体系，多组织，就让用户进行身份选择
						*/
					   this.getOrgAndSySListFunc(data)
					}
				})
			},
			getOrgAndSySListFunc(list) {
				/*
				 * 1.如果组织和体系只有一个，就默认选择登录
				 * 2.多体系，多组织，就让用户进行身份选择
				*/
			   this.toIdentifyChoosePage()
			   if (list.length === 1) {
				   // 如果是单体系单组织的话，静默选择体系组织然后直接跳转工作页面
				   if (list[0].orgList.length === 0) return
				   if (list[0].orgList.length === 1) {
					   this.setHasLogin(true)
					   this.setSysId(list.sysId)
					   this.setOrgId(list[0].orgList[0].orgId)
					   uni.switchTab({
					   	url: `../tabbar/work/work?sysId=${this.currentSysId}&orgId=${this.currentOrgId}`
					   })
				   } else {
					   // 多组织，跳转到身份选择页面
					   this.toIdentifyChoosePage()
				   }
			   } else {
				   // 多体系，跳转到身份选择页面
				   this.toIdentifyChoosePage()
			   }		   
			},
			/**
			 * 跳转多体系多组织身份选择的页面
			 */
			toIdentifyChoosePage() {
				setTimeout(() => {
					uni.navigateTo({
						url: '../identityChoose/identityChoose'
					})	
				}, 1000)
			},
			showToast(title, position = 'top', icon = 'none'){
				uni.showToast({
					icon: icon,
					title: title,
					position: position
				})
			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
